export default {
  name: 'goods',
  data () {
    return {
      searchText: '',
      goodsList: [],
      goodsObj: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      addGoodsDialog: false,
      addGoodsInfo: {
        goods_name: '',
        goods_cat: [{
          value: 'zhinan',
          label: '指南',
          children: [{
            value: 'shejiyuanze',
            label: '设计原则',
            children: [{
              value: 'yizhi',
              label: '一致'
            }, {
              value: 'fankui',
              label: '反馈'
            }, {
              value: 'xiaolv',
              label: '效率'
            }, {
              value: 'kekong',
              label: '可控'
            }]
          }]
        }],
        goods_price: '',
        goods_number: '',
        goods_weight: '',
        goods_introduce: '',
        pics: '',
        attrs: ''
      },
      addGoodsRules: {
        goods_name: [
          { required: true, message: '请填写商品名称', trigger: 'change' },
          { min: 3, message: '最少是3位字符', trigger: 'change' }
        ],
        goods_price: [
          { required: true, message: '请填写商品价格', trigger: 'change' },
          { min: 3, message: '最少是3位字符', trigger: 'change' }
        ],
        goods_weight: [
          { required: true, message: '请填写商品重量', trigger: 'change' },
          { min: 3, message: '最少是3位字符', trigger: 'change' }
        ],
        goods_number: [
          { required: true, message: '请填写商品数量', trigger: 'change' },
          { min: 3, message: '最少是3位字符', trigger: 'change' }
        ]
      },
      total: 0
    }
  },
  methods: {
    async getGoodsList () {
      const { data, meta } = await this.$get('goods', { params: this.goodsObj })
      // console.log(data)
      // console.log(meta)
      if (meta.status === 200) {
        this.goodsList = data.goods
        this.total = data.total
      }
      this.getGoodsList()
    },
    handleSizeChange (val) {
      this.goodsObj.pagesize = val
    },
    handleCurrentChange (val) {
      this.goodsObj.pagenum = val
    },
    addGoodsHandler () {
      this.$refs.addGoods.validate(async valid => {
        if (!valid) {
          this.$message({
            type: 'error',
            message: '请按照规则填写表单'
          })
          return
        }
        console.log(this.addGoodsInfo)
        const { data, meta } = await this.$post('goods', this.addGoodsInfo)
        console.log(data)
        console.log(meta)
        if (meta.status === 400) {
          this.$message({
            type: 'warning',
            message: meta.msg
          })
          return
        }
        this.$message({
          type: 'success',
          message: '添加商品成功'
        })
        this.addGoodsDialog = false
        this.getUsersList()
      })
    }
  },
  created () {
    // console.log(this.goodsList)
    // this.goodsList.forEach(item => {
    //   item.goods_state = item.goods_state === '0' ? '未通过' : (item.goods_state === 1 ? '审核中' : '已审核')
    // })
    // this.goodslist = this.goodsList
    this.getGoodsList()
  },
  watch: {
    goodsObj: {
      handler () {
        this.getGoodsList()
      },
      deep: true
    }
  }
}
